package comxyp.listener;

import comxyp.pojo.settings.Users;
import comxyp.service.settings.UsersService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.util.List;

//该监听器作用是获取用户中信息
@WebListener
public class UserListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent servletContextEvent) {
            //手动从spring容器中获取ProductTypeServiceImpl的对象
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            UsersService usersService = (UsersService) context.getBean("usersServiceImpl");
            //将查询到的商品类别数据存入集合中
            List<Users> userList = usersService.findAll();
            //然后放入全局应用域中 这样当我们要修改 添加 查询功能时都可以从该域中进行获取
            servletContextEvent.getServletContext().setAttribute("userList",userList);
        }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
